<template>
	<view class="settle-box">
		<view class="settle">
			<view class="chooseAll">
				<label @click="changeAllState">
					<radio :checked="isFullCheck" color="#C00000"></radio><text>全选</text>
				</label>
			</view>
			<view>
				<text>合计:</text>
				<text class="payNum">￥{{checkedGoodsAmount}}</text>
			</view>
			<view class="pay" @click="settlement">
				结算({{checkedCount}})
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState,mapGetters,mapMutations } from 'vuex';
	export default {
		name:"my-settle",
		data() {
			return {
				check: true,
				time: null,
				seconds: 3
			};
		},
		computed: {
			...mapState('m_cart', ['SelectAll','cart']),
			...mapState('m_user', ['address','token']),
			...mapGetters('m_cart', ['checkedGoodsAmount','checkedCount','total']),
			...mapGetters('m_user', ['addressInfo']),
			isFullCheck(){
				return this.cart.length?this.checkedCount === this.total:this.check
			}
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['jumpTo']),
			changeAllState(){
				if(!this.cart.length) return this.check = !this.check
				this.updateAllGoodsState(!this.isFullCheck)
			},
			async settlement(){
				if(!this.checkedCount) return uni.$showMsg('请选择商品')
				if(!this.address.provinceName) return uni.$showMsg('请添加收货信息')
				if(!this.token) {
					this.time = setInterval(()=>{
						if(this.seconds <= 0){
							clearInterval(this.time)
							return this.jumpTo({
								url: '/pages/my/my',
								from: '/pages/cart/cart'
							})
						}
						uni.showToast({
							title: "请登录后再结算！"+ this.seconds + '秒后自动跳转到登录页',
							icon: "none",
							mask: true,
							duration: 1500
						})
						this.seconds--
					},1000)
				}
				const orderInfo = {
					// order_price: this.checkedGoodsAmount,
					order_price: 0.01,
					consignee_addr: this.addressInfo,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					}))
				}
				const { data: crateOrderRes } = await uni.$http.post("/my/orders/create", orderInfo)
				if(crateOrderRes.meta.status !== 200) return uni.$showMsg("创建订单失败")
				const orderNumber = crateOrderRes.message.order_number
				
				const { data: getPayData } = await uni.$http.post("/my/orders/req_unifiedorder", {'order_number': orderNumber})
				if(getPayData.meta.status !== 200) return uni.$showMsg('预付订单生成失败')
				const payInfo = getPayData.message.pay
				if(payInfo.err_code_des) return uni.$showMsg(payInfo.err_code_des,'none',2000)
				const requerstPay = await uni.requestPayment(payInfo)
				console.log(requerstPay)
				// if(requerstPay) return uni.$showMsg('订单未支付')
				
				const { data: orderStatus } = await uni.$http.post("/my/orders/chkOrder", {'order_number': orderNumber})
				if(orderStatus.meta.status !== 200) return uni.$showMsg('订单未支付')
				uni.showToast({
					title: "订单支付完成！",
					icon: 'success'
				})
			}
		}
	}
</script>

<style lang="scss">
.settle-box{
	height: 60px;
	.settle{
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: white;
		height: 60px;
		padding: 0 5px;
		z-index: 99;
		.chooseAll{
		}
		.payNum{
			color: #c00000;
		}
		.pay{
			min-width: 100px;
			height: 60px;
			background-color: #C00000;
			color: white;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
}
</style>